package com.longshen.dubbo.api.restful;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.longshen.dubbo.api.anno.LoginRequired;
import com.longshen.dubbo.api.base.Contents;
import com.longshen.dubbo.api.base.ErrorCode;
import com.longshen.dubbo.api.base.RestResult;
import com.longshen.dubbo.api.model.AdminMenu;
import com.longshen.dubbo.api.restful.vo.MenuVOIn;
import com.longshen.dubbo.api.restful.vo.ModeId;
import com.longshen.dubbo.api.restful.vo.DirectoryVo;
import com.longshen.dubbo.api.restful.vo.MenuTreeVo;
import com.longshen.dubbo.api.restful.vo.MenuVO;
import com.longshen.dubbo.api.service.AdminMenuService;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @ClassName: AdminMenuController
 * @Description:系统菜单维护
 * @author: longshen
 * @date: 2019年10月23日 下午2:58:04
 */
@Slf4j
@RequestMapping(value = "/menu")
@RestController
@LoginRequired
public class AdminMenuController {

	@Autowired
	private AdminMenuService adminMenuService;

	/**
	 * 
	 * @Title: getAll @Description: 查询系统菜单列表 @param in @return @return:
	 *         RestResult<UserVO> @throws
	 */
	@RequestMapping(value = "/alllist", method = RequestMethod.GET)
	public RestResult getAll() {
		List<AdminMenu> all = adminMenuService.listAll();

		List<MenuVO> list = buildMenus(all, Contents.MENU_ROOT);
		RestResult rest = RestResult.success();

		rest.setData(list);
		return rest;

	}

	@RequestMapping(value = "/directory", method = RequestMethod.GET)
	public RestResult directory() {

		List<AdminMenu> all = adminMenuService.allDirectory();
		List<DirectoryVo> voList = new ArrayList<DirectoryVo>();
		// 添加更目录
		DirectoryVo root = new DirectoryVo();
		root.setId(Contents.MENU_ROOT);
		root.setMenuname(Contents.MENU_ROOT_NAME);
		voList.add(root);

		if (all != null) {
			all.forEach(m -> {
				DirectoryVo vo = new DirectoryVo();
				vo.setId(m.getId());
				vo.setMenuname(m.getMenuName());
				voList.add(vo);
			});
		}
		RestResult rest = RestResult.success();

		rest.setData(voList);
		return rest;

	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public RestResult addMenu(@RequestBody MenuVOIn in) {
		try {

			AdminMenu add = new AdminMenu();
			add.setComponent(in.getComponent());
			add.setFlag(in.getFlag());
			add.setIcon(in.getIcon());
			add.setMenuName(in.getMenuname());
			add.setMenuPath(in.getMenupath());
			add.setMenuSort(in.getMenusort());
			add.setMenuType(in.getMenutype());
			add.setParentId(in.getParentid());
			adminMenuService.add(add);

			return RestResult.success();
		} catch (DuplicateKeyException e) {
			log.error("添加用户异常", e);
			RestResult rest = RestResult.newInstance();
			rest.setErrorCode(ErrorCode.E_000006);
			rest.setMessage(rest.getMessage() + in.getMenupath());
			return rest;
		} catch (Exception e) {
			return RestResult.fail(ErrorCode.E_999999);
		}

	}

	private List<MenuVO> buildMenus(List<AdminMenu> source, String parentId) {
		if (source != null && !source.isEmpty()) {
			List<MenuVO> list = new ArrayList<MenuVO>();

			source.forEach(value -> {
				if (value.getParentId().equals(parentId)) {
					MenuVO vo = new MenuVO();
					vo.setFlag(value.getFlag());
					vo.setComponent(value.getComponent());
					vo.setIcon(value.getIcon());
					vo.setId(value.getId());
					vo.setMenuname(value.getMenuName());
					vo.setMenupath(value.getMenuPath());
					vo.setMenutype(value.getMenuType());
					vo.setMenusort(value.getMenuSort());
					vo.setParentid(value.getParentId());

					// 当菜单 为目录需要添加子菜单
					if (vo.getMenutype().equals("0")) {
						vo.setChildren(buildMenus(source, value.getId()));
						if (vo.getChildren() != null) {
							vo.setHaschildren(true);
						}

					}
					list.add(vo);
				}
			});
			return list;
		}

		return null;

	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public RestResult del(@RequestBody ModeId in) {
		try {

			adminMenuService.delById(in.getId());
			return RestResult.success();
		} catch (Exception e) {
			return RestResult.fail(ErrorCode.E_999999);
		}

	}

	@RequestMapping(value = "/mod", method = RequestMethod.POST)
	public RestResult mod(@RequestBody MenuVOIn in) {
		try {
			AdminMenu mod = new AdminMenu();
			mod.setComponent(in.getComponent());
			mod.setFlag(in.getFlag());
			mod.setIcon(in.getIcon());
			mod.setMenuName(in.getMenuname());
			mod.setMenuPath(in.getMenupath());
			mod.setMenuSort(in.getMenusort());
			mod.setMenuType(in.getMenutype());
			mod.setParentId(in.getParentid());
			mod.setId(in.getId());
			adminMenuService.modById(mod);
			return RestResult.success();
		} catch (Exception e) {
			return RestResult.fail(ErrorCode.E_999999);
		}

	}

	@RequestMapping(value = "/menutree", method = RequestMethod.GET)
	public RestResult menuTree() {
		List<AdminMenu> all = adminMenuService.listAll();

		List<MenuTreeVo> list = buildMenuTree(all, Contents.MENU_ROOT);
		RestResult rest = RestResult.success();

		rest.setData(list);
		return rest;
	}

	private List<MenuTreeVo> buildMenuTree(List<AdminMenu> source, String parentId) {
		if (source != null && !source.isEmpty()) {
			List<MenuTreeVo> list = new ArrayList<MenuTreeVo>();

			source.forEach(value -> {
				if (value.getParentId().equals(parentId)) {
					MenuTreeVo vo = new MenuTreeVo();

					vo.setId(value.getId());
					vo.setMenuname(value.getMenuName());

					// 当菜单 为目录需要添加子菜单
					if (value.getMenuType().equals("0")) {
						vo.setChildren(buildMenuTree(source, value.getId()));
					}
					list.add(vo);
				}
			});
			return list;
		}

		return null;

	}

}
